function AC_UVA(imnoisy_filename,verbose_plot_path,cs,mu,GVF_ITER,normalize,alpha,beta,tau,SNAKE_ITER,SNAKE_ITER1,init_shape,init_size,im_original);

% Vector field convolution (VFC) external force field example.
% 
%     See also AMT, EXAMPLE_PIG, AM_VFC, AM_VFK, AC_DISPLAY.
% 
%     Reference
%     [1] Bing Li and Scott T. Acton, "Active contour external force using
%     vector field convolution for image segmentation," Image Processing,
%     IEEE Trans. on, vol. 16, pp. 2096-2106, 2007.  
%     [2] Bing Li and Scott T. Acton, "Automatic Active Model
%     Initialization via Poisson Inverse Gradient," Image Processing,
%     IEEE Trans. on, vol. 17, pp. 1406-1420, 2008.   
% 
% (c) Copyright Bing Li 2005 - 2009.


%% parameter settings
disp('Initializing parameters ...')
if 1   %This is to initialize values for the default
    %imnoisy_filename='im_Unoisy.bmp';
    %verbose_plot_path=''
    %cs='vfc'  % The type of prcessing vfc, gvf
    
    %GVF settings
    %mu = .2;
    %GVF_ITER = 100;
    %normalize = 1;
        
    %for the deforming of the vertices
    %alpha = .5;
    %beta = 0;
    %tau = .5;    
    %SNAKE_ITER = 5;
    
    %How many times to run the simulation
    %SNAKE_ITER1 = 60;

    SAVE_WORKING_IMAGE = 1;     % set it to 1 if you want to save the process as a set of images
    SAVE_FINAL_IMAGE = 0;       %save the end result
    DISPLAY_STREAMLINE = 0;     % set it to 1 if you want to plot streamlines, note that it takes a while
    
    clr = {'r' 'r' 'r'};  %plotting color
    
    im_filename='im_U.bmp';  %only for streamline
        
    % circle [cx cy r] centered at [cx cy] with radius r
    %'rect', [xmin xmax ymin ymax]
    %init_shape='circle';   % circle or rect    
    %init_size=[32 32 20];
    RES = .5;  %resolution (distance between vertices) res (not used since no mouse)    

else  %This is for testing 
    imnoisy_filename='im_Unoisy.bmp';
    verbose_plot_path=''
    cs='vfc'  % The type of prcessing vfc, gvf
    
    %GVF settings
    mu = .2;
    GVF_ITER = 100;
    normalize = 1;
    
    %for the deforming of the vertices
    alpha = .5;
    beta = 0;
    tau = .5;    
    SNAKE_ITER = 5;
    
    %How many times to run the simulation
    SNAKE_ITER1 = 60;

    SAVE_WORKING_IMAGE = 1;     % set it to 1 if you want to save the process as a set of images
    SAVE_FINAL_IMAGE = 0;       %save the end result
    DISPLAY_STREAMLINE = 0;     % set it to 1 if you want to plot streamlines, note that it takes a while
    
    clr = {'b' 'b' 'r'};  %plotting color
    
    im_filename='im_U.bmp';  %only for streamline
        
    % circle [cx cy r] centered at [cx cy] with radius r
    %'rect', [xmin xmax ymin ymax]
    init_shape='circle';   % circle or rect    
    init_size=[32 32 20];
    RES = .5;  %resolution (distance between vertices) res (not used since no mouse)    

end

%% Read images
disp('Reading images ...')

if DISPLAY_STREAMLINE  %we only need ground truth if we are displaying the stremaline
    U = imread(im_filename);
end

if ischar(imnoisy_filename)
    noisyU=imread(imnoisy_filename);
else
    noisyU=imnoisy_filename;
end

flag=mkdir(verbose_plot_path)
fig_ext=figure;
fig_ac=figure;


disp(['Examining ' cs])
if ~isempty(verbose_plot_path)
    ext_path=fullfile(verbose_plot_path,'ext');
    ec=mkdir(ext_path);
    if ec~=1
        error(['Unable to create the directory ' ext_path])
    else
      %do nothing    
    end
    ext_filename = 'externalForceField';
    
    iter_path=fullfile(verbose_plot_path,'iter');
    ec=mkdir(iter_path);
    if ec~=1
        error(['Unable to create the directory ' iter_path])
    else
        %do nothing
    end
    iter_idx=0;
    iter_seqlen=ceil(log10(SNAKE_ITER1)); %needed for zero padding
    iter_filename = 'snakeDeformation';
end

    %% compute external force fields
    switch cs,
        case 'gvf', % traditional GVF with Gaussian filter
            disp('--------------------------------------------------')
            disp('Case 1: GVF snake with initial circle close to FOI')
            disp('Computing the external force field ...')
            h = fspecial('gaussian',[5 5],5);
            f = imfilter(double(noisyU),h);
            titl = 'GVF';
            Fext = AM_GVF(f, mu, GVF_ITER, normalize);
 
        case 'vfc', % VFC
            disp('--------------------------------------------------')
            disp('Case 3: VFC snake with initial circle far away from FOI')
            disp('Computing the external force field ...')
            f = noisyU;
            K = AM_VFK(2, 32, 'power',1.8);
            Fext = AM_VFC(f, K, 1);
            titl = 'VFC';
        otherwise
            error(['Invalid active contour type of ' cs]);
    end
  
    %% display
    %I = (1-noisyU)*.3+.7;   % for display
    I = double(1-noisyU)*.3+.7;   % OCTAVE FIX for display
    figure(fig_ext)
    disp('Displaying the external force field ...')

    if DISPLAY_STREAMLINE,   
        clf  %OCTAVE FIX since does not support cla
        [x y] = meshgrid(.5:64,.5:64);
        vt = [x(:) y(:)];   % seeds
        VT = zeros([size(vt) 40]);
        VT(:,:,1) = vt;
        for i=1:39, % moving these seeds
            vt = AC_deform(vt,0,0,tau,Fext,1);
            VT(:,:,i+1) = vt;
        end

        [Ty Tx] = find(~U);  % ground truth  
        hold on
        for i=1:size(vt,1),
            if min(abs(VT(i,1,end)-Tx)+abs(VT(i,2,end)-Ty))<=2,  
                % converge to U-shape
                plot(squeeze(VT(i,1,:)), squeeze(VT(i,2,:)),'r','linewidth',1)
            else
                plot(squeeze(VT(i,1,:)), squeeze(VT(i,2,:)),'k','linewidth',1)
            end
        end
        hold off
        axis equal; axis 'ij';
        axis([1 64 1 64])
    else
        AC_quiver(Fext, I);
        title(['normalized ',titl,' field']);
    end
    
    if SAVE_WORKING_IMAGE
       figure(fig_ext); 
       fn=fullfile(ext_path,[ext_filename '.png']);
       fn=regexprep(fn,'\\','\\');
       print( fn,'-dpng');
    end
    %% uncomment these 2 lines to save the display 
    %     F = getframe(gca);
    %     imwrite(F.cdata,['vector_field',num2str(1),'.bmp']);

    %% initialize a circle at (32 32) with radius R
    disp('Initializing the snake ...')
    vert  = AC_initial(RES, init_shape, init_size);    
    vert0 = vert;

    figure(fig_ac)
    if isempty(im_original)
        imshow(I)
    else
        imshow(im_original);
    end
    AC_display(vert,'close',clr{1});
    drawnow, pause(.5)

    if SAVE_WORKING_IMAGE
       figure(fig_ac); 
       fn=fullfile(iter_path,[iter_filename '_' num2str(iter_idx,['%0' num2str(iter_seqlen) 'd']) '.png']);
       fn=regexprep(fn,'\\','\\');
       print( fn,'-dpng'); iter_idx=iter_idx+1;
    end
    
        
    disp('Deforming the snake ...')
    for i=1:SNAKE_ITER1,
        vert = AC_deform(vert,alpha,beta,tau,Fext,SNAKE_ITER);
        vert = AC_remesh(vert,.5);

        if mod(i,2)==0,
            if isempty(im_original)
                imshow(I)
            else
                imshow(im_original);
            end
                
            h=AC_display(vert,'close',clr{1});
            h=AC_display(vert0,'close',[clr{1} '--']);
            title([titl ' iteration ' num2str(i)])
            drawnow, pause(.5)

            if SAVE_WORKING_IMAGE
                fn=fullfile(iter_path,[iter_filename '_' num2str(iter_idx,['%0' num2str(iter_seqlen) 'd']) '.png']);
                fn=regexprep(fn,'\\','\\');
                figure(fig_ac); print( fn,'-dpng'); iter_idx=iter_idx+1;
            end

         end

    end
    disp('Done!')
    
    
    if SAVE_FINAL_IMAGE
        fn=fullfile('',[iter_filename '_' num2str(iter_idx,['%0' num2str(iter_seqlen) 'd']) '.png']);
        fn=regexprep(fn,'\\','\\');
        figure(fig_ac); print( fn,'-dpng'); iter_idx=iter_idx+1;
    end

